document.addEventListener('DOMContentLoaded', () => {
    const username = localStorage.getItem('username');
    if (!username) {
        window.location.href = 'login.html'; // 如果没有用户名，重定向到登录页面
    }
    document.getElementById('username').innerText = username;
    fetchPapers();
});

function fetchPapers() {
    axios.get('http://localhost:8080/api/papers')
        .then(response => {
            const papersList = document.getElementById('papers-list');
            papersList.innerHTML = '';
            if (response.data.length === 0) {
                papersList.innerHTML = '<li><span>没有论文</span></li>';
            } else {
                response.data.forEach(paper => {
                    const li = document.createElement('li');
                    li.innerHTML = `
                        <div class="paper-info">
                            <div><strong>标题：</strong>${paper.title || '无标题'}</div>
                            <div><strong>作者：</strong>${paper.author || '未知'}</div>
                            <div><strong>摘要：</strong>${paper.abstract1 || '无摘要'}</div>
                            <div class="paper-links">
                                <a href="${paper.pdfurl || '#'}" target="_blank">下载PDF</a>
                            </div>
                            <div class="paper-date">${paper.pubdate || '未知日期'}</div>
                        </div>
                        <div class="actions">
                            <button onclick="editPaper(${paper.id})">编辑</button>
                            <button onclick="deletePaper(${paper.id})">删除</button>
                        </div>
                    `;
                    papersList.appendChild(li);
                });
            }
        })
        .catch(error => {
            console.error('Failed to fetch papers:', error);
            alert('获取论文列表失败，请检查您的网络连接或联系管理员。');
        });
}

// 其他函数（addPaper, editPaper, deletePaper, logout）保持不变
function addPaper() {
    const title = prompt('请输入论文标题:');
    const content = prompt('请输入论文内容:');
    if (title && content) {
        axios.post('http://localhost:8080/api/papers', { title: title, content: content }) // 使用绝对路径
            .then(response => {
                fetchPapers(); // 刷新论文列表
            })
            .catch(error => {
                console.error('Failed to add paper:', error);
                alert('添加论文失败，请检查您的输入或联系管理员。');
            });
    }
}

function editPaper(id) {
    const newTitle = prompt('请输入新的论文标题:');
    const newContent = prompt('请输入新的论文内容:');
    if (newTitle && newContent) {
        axios.put(`http://localhost:8080/api/papers/${id}`, { title: newTitle, content: newContent }) // 使用绝对路径
            .then(response => {
                fetchPapers(); // 刷新论文列表
            })
            .catch(error => {
                console.error('Failed to edit paper:', error);
                alert('编辑论文失败，请检查您的输入或联系管理员。');
            });
    }
}

function deletePaper(id) {
    if (confirm('确定要删除这篇论文吗？')) {
        axios.delete(`http://localhost:8080/api/papers/${id}`) // 使用绝对路径
            .then(response => {
                fetchPapers(); // 刷新论文列表
            })
            .catch(error => {
                console.error('Failed to delete paper:', error);
                alert('删除论文失败，请检查您的网络连接或联系管理员。');
            });
    }
}

function logout() {
    localStorage.removeItem('username');
    window.location.href = 'login.html';
}


